/**
 * 全局Popup
 */
(function() {
	var scripts = document.getElementsByTagName("script")
	var currentScriptPath = scripts[scripts.length-1].src;
	app.directive('zDialog',['$rootScope','$http','$compile','$timeout',function($rootScope,$http,$compile,$timeout) {
		return {
			restrict:'E',
			replace: true,
			templateUrl: currentScriptPath.substring(0, currentScriptPath.lastIndexOf('/') + 1) +"dialog.html",
			scope: {
				id:'@',
				title:'@',
				loadUrl:'@',
				show:'=',
				saveFn:'&',
				submitFn:'&',
				cancelFn:'&',
				haveErrorClass:'=',
				bodyHeight:'=',
				showSaveBtn:'=',
				showSubmitBtn:'=',
				confirmBtnTxt:'@',
				cancelBtnTxt:'@'
			},
			link: function(scope,element,attrs,controller){
				var loadHTML = "";
				var content = element.find("#dialogContent");
				$http.get(scope.loadUrl).success(function(data, status, headers, config) {
					loadHTML = data;
					content.append(loadHTML);
					$compile( content )( scope.$parent );
				});
				element.on("hidden.bs.modal",function() {
					$timeout(function() {
						scope.cancel();
					});
				});
				var options = {
					backdrop: "static",
					show:false
				}
				element.modal(options);
				
				scope.$watch("show",function() {
					if(scope.show==true) {
//						$http.get(scope.loadUrl).success(function(data, status, headers, config) {
//							var options = {
//								backdrop: "static",
//								show:true
//							}
							element.modal('show');
//						});
					} else {
//						content.children().remove();
//						var options = {
//							backdrop: "static",
//							show:false
//						}
//						element.modal(options);
						element.modal('hide');
					}
				});
				
				scope.save = function() {
					if(scope.saveFn) {
						scope.saveFn();
					}
				}
				
				scope.submit = function() {
					if(scope.submitFn) {
						scope.submitFn();
					}
				}
				
				scope.cancel = function() {
					if(scope.cancelFn) {
						scope.cancelFn();
					}
				}
				
			}
		};
	}]);
}());